<?php
include('../data/comm.inc.php');
include('../data/myadminvar.php');
include('../func/func.php');
include('../func/csfunc.php');
include('../func/adminfunc.php');
include('../global/page.class.php');
include('../include.php');
include('./checklogin.php');
switch ($_REQUEST['xtype']) {
    case "show":
        $bid = $_REQUEST['bid'];
        $sid = $_REQUEST['sid'];
        $cid = $_REQUEST['cid'];
        $config['ztype'] = json_decode(transgame($gid,"ztype"),true);
        if ($bid != '' & $sid != '' & $cid != '') {
            $msql->query("select * from `$tb_play` where gid='$gid' and bid='$bid' and sid='$sid' and cid='$cid' order by bid,sid,cid,xsort limit 200");
        } else if ($bid != '' & $sid != '') {
            $msql->query("select * from `$tb_play` where gid='$gid' and  bid='$bid' and sid='$sid'  order by bid,sid,cid,xsort  limit 200");
        } else if ($bid != '') {
            $msql->query("select * from `$tb_play` where gid='$gid' and  bid='$bid'   order by bid,sid,cid,xsort  limit 200");
        } else {
            $msql->query("select * from `$tb_play` where gid='$gid' order by bid,sid,cid,xsort limit 100");
        }
        $i = 0;
        $p = array();
        while ($msql->next_record()) {
            $p[$i]['bid']   = $msql->f('bid');
            $p[$i]['bname'] = transb('name', $msql->f('bid'));
            
            $p[$i]['sid']   = $msql->f('sid');
            $p[$i]['sname'] = transs('name', $msql->f('sid'));
            
            $p[$i]['cid']   = $msql->f('cid');
            $p[$i]['cname'] = transc('name', $msql->f('cid'));
            
            $p[$i]['pid'] = $msql->f('pid');
            
            $p[$i]['name']   = $msql->f('name');
            $p[$i]['ifok']   = $msql->f('ifok');
            $p[$i]['xsort']  = $msql->f('xsort');
            $p[$i]['peilv1'] = $msql->f('peilv1');
            $p[$i]['peilv2'] = $msql->f('peilv2');
            $p[$i]['ztype']  = $config['ztype'][$msql->f('ztype')];
            $p[$i]['znum1']  = $msql->f('znum1');
            $p[$i]['znum2']  = $msql->f('znum2');
            $i++;
        }
        $tpl->assign('p', $p);
        
        $msql->query("select * from `$tb_bclass` where gid='$gid' order by xsort");
        $b = array();
        $i = 0;
        while ($msql->next_record()) {
            $b[$i]['bid']  = $msql->f('bid');
            $b[$i]['name'] = $msql->f('name');
            $i++;
        }
        $tpl->assign("b", $b);
        
        
        if ($bid != '') {
            $msql->query("select * from `$tb_sclass` where gid='$gid' and bid='$bid' order by xsort");
            $s = array();
            $i = 0;
            while ($msql->next_record()) {
                $s[$i]['sid']  = $msql->f('sid');
                $s[$i]['name'] = $msql->f('name');
                $i++;
            }
            $tpl->assign("s", $s);
        }
        
        if ($bid != '' and $sid != '') {
            $msql->query("select * from `$tb_class` where gid='$gid' and bid='$bid'  and sid='$sid' order by xsort");
            $c = array();
            $i = 0;
            while ($msql->next_record()) {
                $c[$i]['cid']  = $msql->f('cid');
                $c[$i]['name'] = $msql->f('name');
                $i++;
            }
            $tpl->assign("c", $c);
        }
        
        
        $tpl->assign("ztype", $config['ztype']);
        
        $tpl->assign('bid', $bid);
        $tpl->assign('sid', $sid);
        $tpl->assign('cid', $cid);
        $tpl->assign('zh', $zh);
        $tpl->display("play.html");
        break;
    
    case "addplay":
        $bid    = $_POST['bid'];
        $sid    = $_POST['sid'];
        $cid    = $_POST['cid'];
        $name   = $_POST['name'];
        $ztype  = ztype(trim($_POST['ztype']));
        $peilv1 = $_POST['peilv1'];
        $peilv2 = $_POST['peilv2'];
        $znum1  = $_POST['znum1'];
        $znum2  = $_POST['znum2'];
        $pid    = setupid($tb_play, 'pid');
        $sql    = "insert into `$tb_play` set gid='$gid',bid='$bid',sid='$sid',cid='$cid',name='$name',ztype='$ztype',znum1='$znum1',znum2='$znum2'";
        $sql .= ",pid='$pid',ifok=1,xsort=0";
        if ($msql->query($sql)) {
            echo 1;
        }
        break;
    case "editplay":  
        $arr = str_replace('\\', '', $_POST['str']);
        $arr = json_decode($arr, true);
        $ca  = count($arr);
		$config['ztype'] = json_decode(transgame($gid,"ztype"),true);
        for ($i = 0; $i < $ca; $i++) {
            $bid    = trim($arr[$i]['bid']);
            $sid    = trim($arr[$i]['sid']);
            $cid    = trim($arr[$i]['cid']);
            $name   = $arr[$i]['name'];
            $ztype  = ztype(trim($arr[$i]['ztype']));
            $peilv1 = $arr[$i]['peilv1'];
            $peilv2 = $arr[$i]['peilv2'];
            $znum1  = $arr[$i]['znum1'];
            $znum2  = $arr[$i]['znum2'];
            $pid    = $arr[$i]['pid'];
            $ifok   = $arr[$i]['ifok'];
            $xsort  = $arr[$i]['xs'];
            
            $sql = "update `$tb_play` set bid='$bid',sid='$sid',cid='$cid',name='$name',ztype='$ztype',znum1='$znum1',znum2='$znum2',peilv1='$peilv1',peilv2='$peilv2'";
            $sql .= ",ifok='$ifok',xsort='$xsort' where pid='$pid' and gid='$gid'";
		
            $msql->query($sql);
            
        }
        echo 1;
        break;
    case "delplay":
        $idstr = $_POST['idstr'];
        $sql   = "delete from `$tb_play` where instr('$idstr',pid) and gid='$gid'";
        if ($msql->query($sql)) {
            echo 1;
        }
        break;


        
}
?>